$().ready(function() {
	validateRule();
	initData();
	initBtn();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
    $("#parentId").removeAttr("disabled");
	$.ajax({
		cache : true,
		type : "POST",
		url : "/sys/menu/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入菜单名称"
			}
		}
	})
}

function initData(){
	//异步加载角色菜单
    $.ajax({
        url:'/sys/menu/selectRoleMenus',
        type:'post',
        data:{},
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if(data){
                var d = JSON.parse(data);
                $(d).each(function(){
                    $("#parentId").append("<option value='"+this.menuId+"'>"+this.name+"</option>");
                });
            }
        }
    });

    //异步加载角色
	var dataObj = {};
	dataObj.offset = 0;
	dataObj.limit = 999999;
    $.ajax({
        url:'/sys/role/getAllRole',
        type:'post',
        data:dataObj,
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if(data){
                var d = JSON.parse(data);
                $(d).each(function(){
                    $("#roleId").append("<option value='"+this.roleId+"'>"+this.roleName+"</option>");
                });
            }
        }
    });
}

function initBtn(){
	$("#type").change(function(){
		if(0 == this.value){
            document.getElementById('parentId').value=this.value;
            $("#parentId").attr("disabled","disabled");
		}else{
            $("#parentId").removeAttr("disabled");
		}
	});
}